﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Dryricenet.houTai
{
    public partial class order_CX : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Refresh();
                int t = Convert.ToInt32(OrderBLL.dataNumBLL());
                double sum = Convert.ToDouble(OrderBLL.sumBLL());
                Label1.Text = "以上数据共" + t + "条，总计：" + sum + "元";
            }
        }
        //刷新
        public void Refresh() {
            GridView1.DataSource = OrderBLL.orderCXBLL();
            GridView1.DataBind();
        }
        //查找
        protected void Search_Click(object sender, EventArgs e)
        {
            string sq = "";
            if (drp.Text == "全部")
            {
                Refresh();
            }
            if (drp.Text == "订单号")
            {
                sq = "and o.Ordertitle like '%" + TextBox1.Text.Trim() + "%'";
            }
            if (drp.Text == "姓名")
            {
                sq = "and o.OrderName like '%" + TextBox1.Text.Trim() + "%'";
            }
            if (drp.Text == "电话")
            {
                sq = "and o.Phone like '%" + TextBox1.Text.Trim() + "%'";
            }
            if (drp.Text == "是否支付")
            {
                string sf = "";
                if (TextBox1.Text.Trim() == "是")
                {
                    sf = "已付款";
                }
                else {
                    sf = "未付款";
                }
                sq = "and o.Zf like '%" + sf + "%'";
            }
            if (drp.Text == "是否发货")
            {
                string sf = "";
                if (TextBox1.Text.Trim() == "是")
                {
                    sf = "已发货";
                }
                else
                {
                    sf = "未发货";
                }
                sq = "and o.Fh like '%" + sf + "%'";
            }
            if (drp.Text == "是否收货")
            {
                string sf = "";
                if (TextBox1.Text.Trim() == "是")
                {
                    sf = "已收货";
                }
                else
                {
                    sf = "未收货";
                }
                sq = "and o.Sh like '%" + sf + "%'";
            }
            

            GridView1.DataSource = OrderBLL.orderMHBLL(sq);
            GridView1.DataBind();
        }
       
        //导出数据表格
        protected void Export_Click(object sender, EventArgs e)
        {
            DataTable dt = OrderBLL.orderCXBLL();
            CreateExcel(dt,"application/ms-excel", "Excel" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".xls");//调用函数
        }
        //分页
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Refresh();
        }
        /// <summary>
        /// DataTable导出到Excel
        /// </summary>
        /// <param name="dt">DataTable类型的数据源</param>
        /// <param name="FileType">文件类型</param>
        /// <param name="FileName">文件名</param>
        public void CreateExcel(DataTable dt, string FileType, string FileName)
        {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
            Response.ContentType = FileType;
            string colHeaders = string.Empty;
            string ls_item = string.Empty;
            DataRow[] myRow = dt.Select();
            int i = 0;
            int cl = dt.Columns.Count;
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                ls_item += dt.Columns[j].ColumnName + "\t"; //栏位：自动跳到下一单元格
            }
            ls_item = ls_item.Substring(0, ls_item.Length - 1) + "\n";
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }
                }
                Response.Output.Write(ls_item);
                ls_item = string.Empty;
            }
            Response.Output.Flush();
            Response.End();
        }
    }
}